/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.api.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.app.param.JiaoyouOrderParam;
import com.yami.shop.bean.model.JiaoyouOrder;
import com.yami.shop.bean.model.User;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.JiaoyouOrderService;
import com.yami.shop.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 创建充值或购买vip订单
 * @author lgh on 2018/10/26.
 */
@RestController
@RequestMapping("/p/order")
@Api(tags = "订单接口")
public class JiaoyouOrderController {

    @Autowired
    private UserService userService;
    @Autowired
    private MapperFacade mapperFacade;
    @Autowired
    private JiaoyouOrderService jiaoyouOrderService;


    @PostMapping
    @ApiOperation(value = "创建订单", notes = "创建充值或购买vip订单")
    public ResponseEntity<JiaoyouOrder> buy(@RequestBody JiaoyouOrderParam param) {
        String userId;
        if (param.getMobile() != null && param.getMobile() != ""){
            userId = userService.getOne(new LambdaQueryWrapper<User>()
                    .eq(User::getUserMobile,param.getMobile())).getUserId();
        }else {
            userId = SecurityUtils.getUser().getUserId();
        }

        JiaoyouOrder order = mapperFacade.map(param, JiaoyouOrder.class);
        order.setUserId(userId);
        order.setOrderNumber(RandomUtil.randomNumbers(32));
        order.setStatus(1);
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        order.setIsPayed(0);
        jiaoyouOrderService.save(order);
        return ResponseEntity.ok(order);
    }


}
